<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>数组运算符</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="language.operators.string.html">« 字符串运算符</a></li>
      <li style="float: right;"><a href="language.operators.type.html">类型运算符 »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="language.operators.html">运算符</a></li>
    <li>数组运算符</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="language.operators.array" class="sect1">
   <h2 class="title">数组运算符</h2>
   <table class="doctable table">
    <caption><strong>数组运算符</strong></caption>
    
     <thead>
      <tr>
       <th>例子</th>
       <th>名称</th>
       <th>结果</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>$a + $b</td>
       <td>联合</td>
       <td><var class="varname">$a</var> 和 <var class="varname">$b</var> 的联合。</td>
      </tr>

      <tr>
       <td>$a == $b</td>
       <td>相等</td>
       <td>如果 <var class="varname">$a</var> 和 <var class="varname">$b</var> 具有相同的键／值对则为 <strong><code>true</code></strong>。</td>
      </tr>

      <tr>
       <td>$a === $b</td>
       <td>全等</td>
       <td>如果 <var class="varname">$a</var> 和 <var class="varname">$b</var> 具有相同的键／值对并且顺序和类型都相同则为 <strong><code>true</code></strong>。</td>
      </tr>

      <tr>
       <td>$a != $b</td>
       <td>不等</td>
       <td>如果 <var class="varname">$a</var> 不等于 <var class="varname">$b</var> 则为 <strong><code>true</code></strong>。</td>
      </tr>

      <tr>
       <td>$a &lt;&gt; $b</td>
       <td>不等</td>
       <td>如果 <var class="varname">$a</var> 不等于 <var class="varname">$b</var> 则为 <strong><code>true</code></strong>。</td>
      </tr>

      <tr>
       <td>$a !== $b</td>
       <td>不全等</td>
       <td>如果 <var class="varname">$a</var> 不全等于 <var class="varname">$b</var> 则为 <strong><code>true</code></strong>。</td>
      </tr>

     </tbody>
    
   </table>

   <p class="para">
    <code class="literal">+</code> 运算符把右边的数组元素附加到左边的数组后面，两个数组中都有的键名，则只用左边数组中的，右边的被忽略。
   </p>
   <p class="para">
    <div class="informalexample">
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$a&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">"a"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"apple"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"b"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"banana"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$b&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">"a"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"pear"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"b"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"strawberry"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"c"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"cherry"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$c&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">+&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;$a&nbsp;和&nbsp;$b&nbsp;的并集<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"Union&nbsp;of&nbsp;\$a&nbsp;and&nbsp;\$b:&nbsp;\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$c</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$c&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$b&nbsp;</span><span style="color: #007700">+&nbsp;</span><span style="color: #0000BB">$a</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;$b&nbsp;和&nbsp;$a&nbsp;的并集<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"Union&nbsp;of&nbsp;\$b&nbsp;and&nbsp;\$a:&nbsp;\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$c</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">+=&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;&nbsp;$a&nbsp;+=&nbsp;$b&nbsp;的并集是&nbsp;$a&nbsp;和&nbsp;$b<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"Union&nbsp;of&nbsp;\$a&nbsp;+=&nbsp;\$b:&nbsp;\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

    </div>
    执行后，此脚本会显示：
    <div class="example-contents screen">
<div class="cdata"><pre>
Union of $a and $b:
array(3) {
  [&quot;a&quot;]=&gt;
  string(5) &quot;apple&quot;
  [&quot;b&quot;]=&gt;
  string(6) &quot;banana&quot;
  [&quot;c&quot;]=&gt;
  string(6) &quot;cherry&quot;
}
Union of $b and $a:
array(3) {
  [&quot;a&quot;]=&gt;
  string(4) &quot;pear&quot;
  [&quot;b&quot;]=&gt;
  string(10) &quot;strawberry&quot;
  [&quot;c&quot;]=&gt;
  string(6) &quot;cherry&quot;
}
Union of $a += $b:
array(3) {
  &#039;a&#039; =&gt;
  string(5) &quot;apple&quot;
  &#039;b&#039; =&gt;
  string(6) &quot;banana&quot;
  &#039;c&#039; =&gt;
  string(6) &quot;cherry&quot;
}
</pre></div>
    </div>
   </p>
   <p class="para">
    数组中的单元如果具有相同的键名和值则比较时相等。
   </p>
   <p class="para">
    <div class="example" id="example-121">
     <p><strong>示例 #1 比较数组</strong></p>
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$a&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">"apple"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"banana"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$b&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"banana"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"0"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"apple"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">==&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;bool(true)<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">===&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;bool(false)<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

    </div>
   </p>

   <div class="sect2">
    <h3 class="title">参见</h3>
    <p class="para">
     <ul class="simplelist">
      <li class="member"><a href="language.types.array.html" class="link">数组类型</a></li>
      <li class="member"><a href="ref.array.html" class="link">数组函数</a></li>
     </ul>
    </p>
   </div>
  </div></div></div></body></html>